const db = require('./db.js');

module.exports.login = (userInfo) =>{
	return db.q('select * from sp_manager where mg_name=? and mg_pwd=? and mg_state=1',userInfo)
}

//查询sp_manager表中总共有多少行数据
module.exports.getMgCount = async (querys) =>{
	if(querys !== ''){
		querys = '%'+querys+'%';
		return await db.q('select count(*) cou from sp_manager where mg_name like ?',querys)
	}else{
		return await db.q('select count(*) cou from sp_manager',null)
	}
}
//依据条件分页查询
module.exports.getMgByQuerys = async (queryInfo) =>{
	let sql='SELECT * FROM sp_manager as mgr LEFT JOIN sp_role as role ON mgr.role_id = role.role_id'
	if(queryInfo[0] !== ''){
		queryInfo[0] = '%'+queryInfo[0]+'%';
		sql += ' where mg_name like ? limit ?,? '
		return await db.q(sql,queryInfo)
	}else{
		sql += ' limit ?,?';
		//删除数组的第一个元素
		queryInfo.splice(0,1);
		return await db.q(sql,queryInfo)
	}
}
//向sp_manager表中添加数据
module.exports.addUser = async (userInfo) =>{
	return await db.q('insert into sp_manager(mg_name,mg_pwd,mg_time,mg_mobile,mg_email) value(?,?,?,?,?)',userInfo)
}
//更改对应用户的状态
module.exports.setMgState = async (stateInfo) =>{
	return await db.q('update sp_manager set mg_state=? where mg_id=?',stateInfo)
}
//修改用户信息
module.exports.editUser = async (editInfo) =>{
	return await db.q('update sp_manager set mg_email=?,mg_mobile=? where mg_id=?',editInfo)
}
//删除用户信息
module.exports.deleteUser = async (uid) =>{
	return await db.q('delete from sp_manager where mg_id=?',uid)
}

module.exports.setMgRole = async (mgInfo) =>{
	return await db.q('update sp_manager set role_id=? where mg_id=?',mgInfo)
}